package com.ipbd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipbd.domain.ThoughtRecord;
import com.ipbd.dto.UserDTO;
import com.ipbd.mapper.ThoughtRecordMapper;
import com.ipbd.service.ThoughtRecordService;
import com.ipbd.utils.UserHolder;
import com.ipbd.vo.ThoughtRecordAllVo;
import com.ipbd.vo.ThoughtRecordVo;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86172
* @description 针对表【thought_record】的数据库操作Service实现
* @createDate 2024-03-06 20:53:48
*/
@Service
public class ThoughtRecordServiceImpl extends ServiceImpl<ThoughtRecordMapper, ThoughtRecord>
    implements ThoughtRecordService {


    @Override
    public List<ThoughtRecordVo> getThoughtRecords() {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 获取当天日期
        LocalDate today = LocalDate.now();

        // 构建查询条件，根据当前用户id和当天日期查询记录
        QueryWrapper<ThoughtRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id)
                .eq("record_date", today);
        List<ThoughtRecord> thoughtRecords = baseMapper.selectList(queryWrapper);

        // 转换为 ThoughtRecordVo 对象列表
        List<ThoughtRecordVo> thoughtRecordVoList = new ArrayList<>();
        for (ThoughtRecord thoughtRecord : thoughtRecords) {
            ThoughtRecordVo thoughtRecordVo = new ThoughtRecordVo();
            thoughtRecordVo.setEvent(thoughtRecord.getEvent());
            thoughtRecordVo.setThought(thoughtRecord.getThought());
            thoughtRecordVo.setEmotion(thoughtRecord.getEmotion());
            thoughtRecordVoList.add(thoughtRecordVo);
        }
        return thoughtRecordVoList;
    }



    @Override
    public void saveThoughtRecords(ThoughtRecordVo thoughtRecordVo) {
        // 获取用户id
        Integer id = UserHolder.getUser().getId();
        // 获取当天日期
        LocalDate today = LocalDate.now();

        // 将 ThoughtRecordVo 转换为 ThoughtRecord
        ThoughtRecord thoughtRecord = new ThoughtRecord();
        thoughtRecord.setUserId(id);
        thoughtRecord.setRecordDate(Date.valueOf(today));
        thoughtRecord.setEvent(thoughtRecordVo.getEvent());
        thoughtRecord.setThought(thoughtRecordVo.getThought());
        thoughtRecord.setEmotion(thoughtRecordVo.getEmotion());

        // 插入数据库
        baseMapper.insert(thoughtRecord);
    }
}




